package com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.decorator;

import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.Quackable;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.observer.Observable;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.observer.Observer;

/**
 * 鸭子行为装饰器
 * 统计叫声数量
 * @author 、
 */
public class QuackCounter implements Quackable {

    private Quackable quackable;

    /**
     * 这里要声明为静态方法是因为对所有的 QuackCounter 一起做统计
     */
    private static int numberOfQuacks;

    public QuackCounter(Quackable quackable) {
        this.quackable = quackable;
    }

    @Override
    public void quack() {
        quackable.quack();
        // 装饰 quack() 方法
        numberOfQuacks++;
    }

    public static int getNumberOfQuacks() {
        return numberOfQuacks;
    }

    @Override
    public void registerObserver(Observer observer) {
        quackable.registerObserver(observer);
    }

    @Override
    public void notifyObserver() {
        quackable.notifyObserver();
    }
}
